package action.ManagerAction;


import com.opensymphony.xwork2.ActionSupport;
import common.StringUtils;
import entity.CroRole;
import entity.CroUser;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.hibernate.procedure.spi.ParameterRegistrationImplementor;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroRoleService;
import service.CroUserService;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.UUID;

/**
 * Created by xuweijie on 17-5-8.
 * 后台用户管理
 */
public class UserAction extends ActionSupport {

    private List<CroUser> users;
    private String userid;
    private String successResult;
    private List<CroRole> croRoles;
    private String username;
    private String password;
    private String phone;
    private String roleid;
    private String sex;

    @Autowired
    private CroUserService croUserService;
    @Autowired
    private CroRoleService croRoleService;

    /**
     * 显示用户详细信息和全部角色
     * */
    @RequiresPermissions("user:select")
    public String detail(){
        users=croUserService.findAllUser();
        croRoles=croRoleService.FindAllRole();
        return SUCCESS;
    }

    /*
    * 删除用户
    * */
    @RequiresPermissions("user:delete")
    public String deleteuser(){
        if (croUserService.deleteUser(userid)){
            successResult = "删除成功！";
        }else{
            successResult = "删除失败！";
        }
        return SUCCESS;
    }

    /**
     * 添加用户
     * */
    @RequiresPermissions("user:write")
    public String insertuser(){
        String slat= UUID.randomUUID().toString().substring(0,8);
        CroRole croRole=croRoleService.FindRoleById(roleid);
        CroUser croUser=new CroUser();
        croUser.setUsername(username);
        croUser.setPassword(new Md5Hash(password,slat).toString());
        croUser.setPhone(phone);
        croUser.setSex(sex);
        croUser.setCroRole(croRole);
        croUser.setSlat(slat);
        if(croUserService.insertUser(croUser)){
            successResult = "添加成功！";
        }else {
            successResult = "添加失败！";
        }
        return SUCCESS;
    }

    /**
     * 更新用户
     * */
    @RequiresPermissions("user:update")
    public String updateuser(){
        String slat=null;
        String newpassword = null,checkpassword=null;
        ServletContext context = ServletActionContext.getServletContext();
        String susername= (String) context.getAttribute("username");
        CroUser croUser=croUserService.FindUserById(userid);
        if(StringUtils.isNotBlank(password)){
            slat= UUID.randomUUID().toString().substring(0,8);
            newpassword=new Md5Hash(password,slat).toString();
            checkpassword=new Md5Hash(password,croUser.getSlat()).toString();
        }
        if(StringUtils.isNotBlank(checkpassword) && checkpassword.equals(croUser.getPassword())) {
            successResult = "0";
        }else{
            croUser.setSex(sex);
            croUser.setPhone(phone);
            if(StringUtils.isNotBlank(newpassword) && StringUtils.isNotBlank(slat)){
                croUser.setPassword(newpassword);
                croUser.setSlat(slat);
            }
            CroRole croRole=croRoleService.FindRoleById(roleid);
            croUser.setCroRole(croRole);
            if(croUserService.updateUser(croUser)){
                if(croUser.getUsername().equals(susername) && StringUtils.isNotBlank(password)){
                    successResult="1";
                }else {
                    successResult="2";
                }
            }else {
                successResult="3";
            }
        }
        return SUCCESS;
    }



    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public List<CroUser> getUsers() {
        return users;
    }

    public void setUsers(List<CroUser> users) {
        this.users = users;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public List<CroRole> getCroRoles() {
        return croRoles;
    }

    public void setCroRoles(List<CroRole> croRoles) {
        this.croRoles = croRoles;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRoleid() {
        return roleid;
    }

    public void setRoleid(String roleid) {
        this.roleid = roleid;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}
